<template>
  <div id="div_box_nav">
    <van-nav-bar
  title="登录"
  left-arrow
  @click-left="$router.back()"
/>
  <van-form @submit="onSubmit" >
  <van-field
    v-model="username"
    name="username"
    placeholder="用户名"
    :rules="[{ required: true, message: '请填写用户名' }]"
  />
  <van-field
    v-model="password"
    type="password"
    name="password"
    placeholder="密码"
    :rules="[{ required: true, message: '请填写密码' }]"
  />
  <div style="margin: 16px;">
    <van-button round block type="info" native-type="submit">登录</van-button>
  </div>
  <router-link to="/register" class="link" >没有账号?注册账号</router-link>
</van-form>

</div>
</template>

<script>

import { LoginFn } from '@/api/user'
import { setToken } from '@/utils/storage'
export default {
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async  onSubmit (values) {
      // console.log('submit', values)
      const data = await LoginFn(values)// 请求成功
      // 获取token存到本地
      console.log(data)
      setToken(data.body.token)
      // 提示框
      this.$toast(data.description)
      this.$router.push('/')
    },
    onClickLeft () {

    }
  }
}
</script>

<style lang="less" scoped>
.link {
  color: #069;
  font-size: 12px;
  padding-right: 20px;
  float: right;

}
.van-nav-bar {
background-color:#21b97a;
}
#div_box_nav /deep/ .van-nav-bar__title {
  color: white;
}

</style>
